package com.jingdianjichi.subject.application.controller;

import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectAnswerDTO;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.application.dto.SubjectInfoQueryDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;

import com.jingdianjichi.subject.domain.convert.SubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoQueryBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {


    @Resource
    SubjectInfoDomainService subjectInfoDomainService;
    /**
     * 添加题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody @Validated SubjectInfoDTO subjectInfoDTO){
        log.info("添加题目: {}", subjectInfoDTO);
        List<SubjectAnswerDTO> subjectAnswerDTOList = subjectInfoDTO.getSubjectAnswerDTOList();
        SubjectInfoBO subjectInfoBO= SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        List<SubjectAnswerBO> subjectAnswerBOList =SubjectInfoDTOConverter.INSTANCE.convertAnswerDTOToBO(subjectAnswerDTOList);
        subjectInfoBO.setSubjectAnswerDTOList(subjectAnswerBOList);

        boolean flag = subjectInfoDomainService.add(subjectInfoBO);
        return Result.ok(flag);
    }

    /**
     * 查询分类下 标签下的题目
     */
    @PostMapping("/queryPageInfo")
    public Result<PageResult<SubjectInfoDTO>> queryPageInfo(@RequestBody @Validated SubjectInfoQueryDTO subjectInfoQueryDTO){
        log.info("查询题目: {}", subjectInfoQueryDTO);
        SubjectInfoQueryBO subjectInfoQueryBO = new SubjectInfoQueryBO();
        BeanUtils.copyProperties(subjectInfoQueryDTO,subjectInfoQueryBO);

        PageResult<SubjectInfoBO> result = subjectInfoDomainService.queryPageInfo(subjectInfoQueryBO);
        log.info("分页查询题目列表Result: {}", result.getResult());
        List<SubjectInfoBO> subjectInfoBOS = result.getResult();
        List<SubjectInfoDTO> subjectDTOList = SubjectInfoDTOConverter.INSTANCE.convertBOListToDTOList(subjectInfoBOS);
        //PageResult<SubjectInfoDTO> DTOResult = SubjectInfoDTOConverter.INSTANCE.convertPageBOToDTO(result);
        //log.info("分页查询题目列表: {}", DTOResult.getResult());

        PageResult<SubjectInfoDTO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoQueryBO.getPageNo());
        pageResult.setPageSize(subjectInfoQueryBO.getPageSize());
        pageResult.setRecords(subjectDTOList);
        log.info("分页查询题目列表DTO: {}", subjectDTOList);
        return Result.ok(pageResult);
    }


    /**
     * 根据id查询题目详细信息
     */
    @PostMapping("/queryInfoById")
    public Result<SubjectInfoDTO> queryInfoById(@RequestBody SubjectInfoQueryDTO subjectInfoQueryDTO){
        log.info("查询题目: {}", subjectInfoQueryDTO);
        SubjectInfoQueryBO subjectInfoQueryBO = new SubjectInfoQueryBO();
        BeanUtils.copyProperties(subjectInfoQueryDTO,subjectInfoQueryBO);

        SubjectInfoBO result = subjectInfoDomainService.queryInfoByAllIds(subjectInfoQueryBO);
        SubjectInfoDTO subjectInfoDTO1 = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(result);
        return Result.ok(subjectInfoDTO1);
    }


}
